#include "uart.c"

/**
 * 屏幕循环输出一个数字。
 * 实现两个按键，按键1按下，将屏幕输出的数字-1
 * 按键2按下，将屏幕输出的数字+1
 *
 * 实现按键持续按下的效果，如果持续按下，屏幕每次输出3次数字后，数字变化
 */

void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for (i = 0; i < ms; i++)
    {
        for (j = 0; j < 123; j++)
            ;
    }
}
int i=0;
int a;
sbit BTN1=P2^0;
sbit BTN2=P2^7;

#define LONG_PRESS_TIME_MS 50

typedef enum
{
    PRESSED,       // 按键被按下
    RELEASED,      // 按键释放，作为按键次数的计数
} KeyState;

void main()
{
    KeyState state = RELEASED;
    unsigned int pressed_time_counter = 0;


    uart_init();

    while (1)
    {
        switch(state)
        {
            case PRESSED:
                if(BTN1 == 0)
                {
                    if(pressed_time_counter >= LONG_PRESS_TIME_MS)
                    {
                        pressed_time_counter = 0;
                        i++;
                    }else{
                        pressed_time_counter++;
                        printf("%d\n",i);
                        delay_ms(500);
                    }
                }else{
                    state=RELEASED;
                }
            case RELEASED:
                if(BTN1 == 1)
                {
                    pressed_time_counter = LONG_PRESS_TIME_MS + 1;
                }else{
                    state = PRESSED;
                }
            break;
        }
    }
}